#pragma once
#include "util.hpp"

class Mutex
{
public:
    Mutex()
    {
        pthread_mutex_init(&_lock,nullptr);
    }

    ~Mutex()
    {
        pthread_mutex_destroy(&_lock);
    }

    void lock()
    {
        pthread_mutex_lock(&_lock);
    }

    void unlock()
    {
        pthread_mutex_unlock(&_lock);
    }
private:
    pthread_mutex_t _lock;
};


class LockGuard
{
public:
    LockGuard(Mutex* mutex)
        :_mutex(mutex)
    {
        _mutex->lock();
        cout << "加锁成功" <<endl;;
    }

    ~LockGuard()
    {
        _mutex->unlock();
        cout << "解锁成功" << endl;
    }

private:
    Mutex* _mutex;
};
